from myelin.Ns import MYELIN
from myelin.rdf.Ns import RDFS_SEEALSO, RDFS_ISDEFINEDBY, OWL_SAMEAS
from myelin.rdf.bindery.Model import OBJECT_TYPE_RESOURCE, OBJECT_TYPE_LITERAL
from myelin.server.core.ft import ModelDriver

from nodetypes.ntypes.Resource.view.RDF import Transform, Document


class Document(Document):    
    
    newContentDriver = staticmethod(ModelDriver.FromRepo)
    
    def getStatements(self, context, consumer):
        model = context.model
        model.rdf_prefetch()
        
        locale = model.dcLanguage
        locale = locale and locale[0].rdf_value or context.defaults.default_locale
                        
        return self.getMetaStatements(
            context,
            self.getRelatedStatements(
                context,
                consumer,
                self.getRelatedDriver(context, model),
                locale
            ),
            model,
            locale
        )
    
    def getRelatedDriver(self, context, model):
        driver = context.request.graphs.get(
            context.mode
        )
        #print "%s, %s" % (self.mode, str(driver))
        if driver:
            driver = self.newContentDriver(
                context.repo,
                driver
            )
            if driver != model.rdf_dbDriver:
                return model.rdf_initDriver(
                    driver
                )
        return model.rdf_dbDriver
        
    def getMetaStatements(self, context, consumer, driver, locale):
        root = context.defaults.document_root
        model = context.model
        
        mrs = model.rdf_toStatements
        
        consumer(mrs('rdfType'))
        consumer(mrs('mTag'))
        consumer(mrs('dcIdentifier'))
        consumer(mrs('dcTitle'))
        consumer(mrs('dcLanguage'))
        consumer(mrs('dcDescription'))
        consumer(mrs('dcSubject'))
        consumer(mrs('dcFormat'))
        consumer(mrs('dcType'))
        consumer(mrs('mEffective_Date'))
        consumer(mrs('mExpiry_Date'))
        
        consumer(mrs('dctermsCreated'))
        consumer(mrs('dcCreator'))
        for m in model.dcCreator:
            self.getAgentStatements(
                context,
                consumer,
                m,
                locale,
                #root
            )
        
        consumer(mrs('dctermsModified'))        
        consumer(mrs('mLast_Modifier'))
        for m in model.mLast_Modifier:    
            self.getAgentStatements(
                context,
                consumer,
                m,
                locale,
                #root
            )
            
        uri = context.urlFormatter.getPublicUrl(model.rdf_domain, True)
        if uri:
            consumer((
                (
                    model.rdf_value,
                    RDFS_ISDEFINEDBY,
                    uri,
                    None,
                    model.rdf_domain,
                    OBJECT_TYPE_RESOURCE,
                ),
            ))
        
        # misc
        return model.rdf_consumeMiscStatements(consumer)
        
    def getRelatedStatements(self, context, consumer, driver, locale):
        
        self.getParentStatements(
            context,
            consumer,
            driver,
            locale
        )
        
        self.getGroupingStatements(
            context,
            consumer,
            driver,
            locale
        )
                        
        return consumer
    
    def getParentStatements(self, context, consumer, driver, locale):        
            
        acls = context.acls.checkNodeAcls
        action = context.action
        model = context.model
        id = model.rdf_value
        root_id = context.defaults.root_id  
        
        href = context.urlFormatter.newUrlMapper(True)
        for np,n in model.rdf_toRelations('mParent'):
            n = n.rdf_clone(driver)
            if n.rdf_getDomain() == n.rdf_scope:
                continue
            if acls(n.rdf_domain, action):
                nrs = n.rdf_toStatements
                consumer(nrs('rdfType'))
                consumer(nrs('dcTitle'))
                consumer(nrs('dcLanguage'))
                consumer((
                    n.rdf_asStatement(
                        np,
                        id
                    ),
                ))
                o = href(n.rdf_domain)
                print "PARENT: ", n.rdf_domain, n.rdf_value
                print " murl: ", o
                if o:
                    consumer((
                        (
                            n.rdf_value,
                            RDFS_SEEALSO,
                            o,
                            '',
                            n.rdf_domain,
                            OBJECT_TYPE_RESOURCE,
                        ),
                    ))
            
        for t in model.mTranslation_Of:
            t = t.rdf_clone(driver)
            tid = t.rdf_value 
            for np,n in t.rdf_toRelations('mParent'):
                if locale in n.dcLanguage and \
                    acls(n.rdf_domain, action):
                    nrs = n.rdf_toStatements
                    
                    consumer(nrs('rdfType'))
                    consumer(nrs('dcTitle'))
                    consumer(nrs('dcLanguage'))
                    
                    consumer((
                        n.rdf_asStatement(
                            np,
                            tid
                        ),
                    ))
                    
                    o = href(n.rdf_domain)
                    if o:
                        consumer((
                            (
                                n.rdf_value,
                                RDFS_SEEALSO,
                                o,
                                '',
                                n.rdf_domain,
                                OBJECT_TYPE_RESOURCE,
                            ),
                        ))
                    
                else:
                    mhit = 0
                    nid = n.rdf_value
                    #n = n.rdf_clone(self.contentDriver)
                    for mp,m in n.rdf_toRelations('mTranslation'):
                        if locale in m.dcLanguage and \
                            acls(m.rdf_domain, action):
                            
                            mrs = m.rdf_toStatements                            
                            consumer(mrs('rdfType'))
                            consumer(mrs('dcTitle'))
                            consumer(mrs('dcLanguage'))
                            
                            consumer((
                                m.rdf_asStatement(
                                    mp,
                                    nid
                                ),
                            ))
                            
                            o = href(n.rdf_domain)
                            if o:
                                consumer((
                                    (
                                        m.rdf_value,
                                        RDFS_SEEALSO,
                                        href(m.rdf_domain),
                                        '',
                                        m.rdf_domain,
                                        OBJECT_TYPE_RESOURCE,
                                    ),
                                ))
                            
                            mhit = 1
                            
                    if mhit:     
                        consumer((
                            n.rdf_asStatement(
                                np,                                
                                tid
                            ),
                        ))
                        nrs = t.rdf_toStatements
                        
                        consumer(nrs('rdfType'))   
                        consumer(nrs('dcTitle'))
                        consumer(nrs('dcLanguage'))
                        
                        o = href(n.rdf_domain)
                        if o:
                            consumer((
                                (
                                    t.rdf_value,
                                    RDFS_SEEALSO,
                                    o,
                                    '',
                                    t.rdf_domain,
                                    OBJECT_TYPE_RESOURCE,
                                ),
                            ))
                        
        return consumer
        
        
    def getGroupingStatements(self, context, consumer, driver, locale):
        acls = context.acls.checkNodeAcls
        action = context.action
        model = context.model
        mid = model.rdf_value
        
        href = context.urlFormatter.newUrlMapper(True)
        for np,n in model.rdf_toRelations('mGrouping'):
            n = n.rdf_clone(driver)
            if n.rdf_getDomain() and acls(n.rdf_domain, action):
                nrs = n.rdf_toStatements
                
                consumer(nrs('rdfType'))
                consumer(nrs('dcTitle'))
                consumer(nrs('dcLanguage'))
                
                consumer((
                    n.rdf_asStatement(
                        np,
                        mid
                    ),
                ))
                
                o = href(n.rdf_domain)
                if o:
                    consumer((
                        (
                            n.rdf_value,
                            RDFS_SEEALSO,
                            o,
                            '',
                            n.rdf_domain,
                            OBJECT_TYPE_RESOURCE,
                        ),
                    ))
        
        for tp,t in model.rdf_toRelations('mTranslation_Of'):
            t = t.rdf_clone(driver)
            thit = 0
            tid = t.rdf_value
            for gp,g in t.rdf_toRelations('mGrouping'):
                ghit = 0
                gid = g.rdf_value
                for np,n in g.rdf_toRelations('mTranslation'):
                    if locale in n.dcLanguage and \
                        acls(n.rdf_domain,action):
                    
                        nrs = n.rdf_toStatements
                        
                        consumer(nrs('rdfType'))
                        consumer(nrs('dcTitle'))
                        consumer(nrs('dcLanguage'))
                    
                        consumer((
                            n.rdf_asStatement(
                                np,
                                gid
                            ),
                        ))
                        
                        o = href(n.rdf_domain)
                        if o:
                            consumer((
                                (
                                    n.rdf_value,
                                    RDFS_SEEALSO,
                                    href(n.rdf_domain),
                                    '',
                                    n.rdf_domain,
                                    OBJECT_TYPE_RESOURCE,
                                ),
                            ))
                        
                        ghit = 1
                        
                if ghit:
                    consumer((
                        g.rdf_asStatement(
                            gp,
                            tid
                        ),
                    ))
                    
                    nrs = g.rdf_toStatements
                        
                    consumer(nrs('rdfType'))  
                    consumer(nrs('dcTitle'))
                    consumer(nrs('dcLanguage'))
                    
                    o = href(n.rdf_domain)
                    if o:
                        consumer((
                            (
                                g.rdf_value,
                                RDFS_SEEALSO,
                                href(g.rdf_domain),
                                '',
                                g.rdf_domain,
                                OBJECT_TYPE_RESOURCE,
                            ),
                        ))
                        
                    thit = 1
            
            if thit:
                consumer((
                    t.rdf_asStatement(
                        tp,
                        mid
                    ),
                ))
                
                nrs = t.rdf_toStatements
                        
                consumer(nrs('rdfType'))  
                consumer(nrs('dcTitle'))
                consumer(nrs('dcLanguage'))
                
                o = href(n.rdf_domain)
                if o:
                    consumer((
                        (
                            t.rdf_value,
                            RDFS_SEEALSO,
                            href(t.rdf_domain),
                            '',
                            t.rdf_domain,
                            OBJECT_TYPE_RESOURCE,
                        ),
                    ))
        
        return consumer
    


class Transform(Transform):
    
    newDocument = Document
    
    
# cleanup
del ModelDriver